package com.itany.nmms.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itany.mvc.annotation.RequestMapping;
import com.itany.mvc.annotation.ResponseBody;
import com.itany.nmms.constant.DictConstant;
import com.itany.nmms.constant.ResponseCodeConstant;
import com.itany.nmms.entity.ProductType;
import com.itany.nmms.exception.ProductTypeExistException;
import com.itany.nmms.exception.RequestParameterErrorException;
import com.itany.nmms.exception.ServiceException;
import com.itany.nmms.factory.ObjectFactory;
import com.itany.nmms.service.ProductTypeService;
import com.itany.nmms.util.ParameterUtil;
import com.itany.nmms.util.ResponseResult;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Author:shixiaojun@itany.com
 * Date:2022/4/28 9:13
 * Version:1.0-SNAPSHOT
 * Description:
 */
@RequestMapping("/type")
public class ProductTypeController {

    private ProductTypeService typeService = (ProductTypeService) ObjectFactory.getObject("typeService");

    @RequestMapping("/findAll")
    public String findAll(HttpServletRequest request, HttpServletResponse response){
        // 分页
        // 获取前台需要的页码信息
        String pageNo = request.getParameter("pageNo");
        String pageSize = request.getParameter("pageSize");
        // 如果前台没有传递页码信息,需要后台开发员工设置一个默认值
        if(ParameterUtil.isNull(pageNo)){
            pageNo = DictConstant.PRODUCT_TYPE_DEFAULT_PAGE_NO;
        }
        if(ParameterUtil.isNull(pageSize)){
            pageSize = DictConstant.PRODUCT_TYPE_DEFAULT_PAGE_SIZE;
        }

        try {
            // 分页三部曲
            // 1.设置分页信息
            // 当设置分页信息之后,所遇到的所有数据均会被分页
            // 因此,当某些数据不需要分页的时候,必须在设置分页信息之前进行查询
            PageHelper.startPage(Integer.parseInt(pageNo),Integer.parseInt(pageSize));
            // 2.获取业务数据
            // 在此处获取的业务数据将会被分页
            List<ProductType> types = typeService.findAll();
            // 3.封装分页对象
            PageInfo<ProductType> typePage = new PageInfo<>(types);
            request.setAttribute("typePage",typePage);
        } catch (ServiceException e) {
            request.setAttribute("typeMsg",e.getMessage());
        } catch (Exception e) {
            request.setAttribute("typeMsg","服务器内部异常");
        }
        return "backend/productTypeManager";
    }

    // @ResponseBody注解标注在方法上
    // 表示当前方法不再返回页面
    // 而是将方法的返回值作为数据模型来返回
    // 即Ajax操作
    // 返回值如果是对象,会自动帮我们转换成JSON
    @ResponseBody
    @RequestMapping("/addType")
    public ResponseResult addType(HttpServletRequest request, HttpServletResponse response){
        ResponseResult result = new ResponseResult();
        String name = request.getParameter("name");
        try {
            typeService.addType(name);
            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        } catch (ProductTypeExistException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        } catch (ServiceException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }

}
